package csu.fun.spring_store.web.controller;

import csu.fun.spring_store.config.FileUtils;
import csu.fun.spring_store.domain.*;
import csu.fun.spring_store.persistence.CategoryMapper;
import csu.fun.spring_store.persistence.InventoryMapper;
import csu.fun.spring_store.persistence.ItemMapper;
import csu.fun.spring_store.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Validated
@SessionAttributes("Product")
//商品管理
public class ProductController {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @RequestMapping("/newcate")
    public String newcate(){
        return "newcategory";
    }
    @RequestMapping("/newproduct")
    public String newproduct(@RequestParam(value = "category")String category,Model model ){
        model.addAttribute("category",category);
        return "newproduct";
    }
    @RequestMapping("/newitem")
    public String newitem(@RequestParam(value = "productid")String productid, Model model){
        model.addAttribute("productid", productid);
        return "newitem";
    }
    @RequestMapping("/creatcate")
    public String creatcate(@RequestParam(value="name")String name){
        System.out.println(name);
        return "redirect:GoodsForm";
    }
    @RequestMapping("/productlist")
    public String productlist(Model model, @RequestParam(value="id")String id){
       List<Product> productList=productMapper.selectBycatid(id);
       model.addAttribute("productlist",productList);
       id="/newproduct?category="+id;
       model.addAttribute("id",id);
        return "GoodsProduct";
    }
    @RequestMapping("/itemlist")
    public String itemlist(Model model, @RequestParam(value="id")String id){
        List<Item> itemList=itemMapper.selectByproductid(id);
        model.addAttribute("productlist",itemList);
        id="/newitem?productid="+id;
        model.addAttribute("id",id);
        return "GoodsItem";
    }

    /**
     *
     * @param file 要上传的文件
     * @return
     */
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String upload(@RequestPart("file") MultipartFile file, Map<String, Object> map,@RequestPart(value = "id") String id){

        // 要上传的目标文件存放路径
        String localPath = "D:/Develop/Files/Photos";
        System.out.println("2222");
        // 上传成功或者失败的提示
        String msg = "";

        if (FileUtils.upload(file, localPath, file.getOriginalFilename())){
            // 上传成功，给出页面提示
            msg = "上传成功！";
        }else {
            msg = "上传失败！";

        }

        // 显示图片
        map.put("msg", msg);
        map.put("fileName", file.getOriginalFilename());

        return file.getOriginalFilename();
    }
    @RequestMapping("/categorybianji")
    public String categorybianji(@RequestParam(value = "id")String id,Model model){
        Category category=categoryMapper.getCategoryById(id);
        model.addAttribute("category",category);

        return "CategoryBianji";
    }
    @RequestMapping("/productbianji")
    public String productbianji(@RequestParam(value = "id")String id,Model model){
        Product product=productMapper.getProductById(id);
        model.addAttribute("product",product);

        return "ProductBianji";
    }
    @RequestMapping("/itembianji")
    public String itembianji(@RequestParam(value = "id")String id,Model model){
        Item item=itemMapper.getItemByItemid(id);
        model.addAttribute("item",item);
        Inventory inventory=inventoryMapper.selectById(id);
        model.addAttribute("inventory",inventory);
        return "ItemBianji";
    }
    @RequestMapping("/updateitem")
    public String updateitem(Item item,@RequestParam(value = "qty")int qunatity){
        itemMapper.updateById(item);
        Inventory inventory=new Inventory();
        inventory.setItemid(item.getItemid());
        inventory.setQuantity(qunatity);
        inventoryMapper.updateById(inventory);
        return "redirect:/itemlist?id="+item.getProductid();
    }
    @RequestMapping("/newcatetijiao")
    public String newcatetijiao(Category category){
        categoryMapper.insert(category);
        return "redirect:/GoodsForm";
    }
    @RequestMapping("/newproducttijiao")
    public String newproducttijiao(Product product){
        productMapper.insert(product);
        return "redirect:/productlist?id="+product.getCategory();
    }
    @RequestMapping("/newitemtijiao")
    public String newitemtijiao(Item item){
        itemMapper.insert(item);
        return "redirect:/itemlist?id="+item.getProductid();
    }

//    @GetMapping("/GoodsForm")
//    public String GoodsForm(Model model) {
//        List<Product> products = productMapper.selectList(null);
//        model.addAttribute("Product",products);
//        return "GoodsManagement.html";
//    }


    @PostMapping("/update1")
    public String update1(@ModelAttribute Product product,
                         Model  model){

        productMapper.setCategoryByProductid(product.getProductid(),product.getCategory());
        productMapper.setNameByProductid(product.getProductid(),product.getName());
        productMapper.setDescnByProductid(product.getProductid(),product.getDescn());
        productMapper.setSumByProductid(product.getProductid(), product.getSum());

        List<Product> products=productMapper.selectList(null);
        model.addAttribute("Product",products);
        return "GoodsManagement.html";

    }
    @RequestMapping("/catetijiao")
    public String tijiao(Category category){
        categoryMapper.updateById(category);
        return "redirect:/GoodsForm";
    }
    @RequestMapping("/producttijiao")
    public String producttijiao(Product product){
        productMapper.updateById(product);

        return "redirect:/productlist?id="+product.getCategory();
    }
    @RequestMapping("/ItemManagement")
    public String item(Model model)
    {
        List<Item> itemList=itemMapper.selectList(null);
        model.addAttribute("itemlist",itemList);
        List<Inventory> inventoryList =inventoryMapper.selectList(null);
        model.addAttribute("inventorylist",inventoryList);
        return "ItemManagement";
    }
    @RequestMapping("/Itemlose")
    public String reitem(Model model)
    {
        List<Item> itemList=itemMapper.selectList(null);
        model.addAttribute("itemlist",itemList);
        return "xiajia";
    }
    @RequestMapping("/wencatexiajia")
    @ResponseBody
    public String wencatexiajia(@RequestParam(value = "id")String id){
        String gj="";
       List<Product> productList= productMapper.selectBycatid(id);
       for(int i=0;i<productList.size();i++)
       {
           List<Item> itemList=itemMapper.selectByproductid(productList.get(i).getProductid());
           for(int u=0;u<itemList.size();u++)
           {

               gj=gj+","+itemList.get(u).getItemid();
           }

       }
       return gj;
    }
    @RequestMapping("/catexiajia")
    @ResponseBody
    public String catexiajia(@RequestParam(value = "id")String id){

        List<Product> productList= productMapper.selectBycatid(id);
        for(int i=0;i<productList.size();i++)
        {
            List<Item> itemList=itemMapper.selectByproductid(productList.get(i).getProductid());
            for(int u=0;u<itemList.size();u++)
            {
                itemMapper.itemxiajia(itemList.get(u).getItemid());
            }

        }
        return "下架成功";
    }
    @RequestMapping("/wenproductxiajia")
    @ResponseBody
    public String wenproductxiajia(@RequestParam(value = "id")String id){
        String hj="";
        List<Item> itemList=itemMapper.selectByproductid(id);
        for(int u=0;u<itemList.size();u++)
        {
            hj=hj+","+itemList.get(u).getItemid();
        }
        return hj;
    }
    @RequestMapping("/productxiajia")
    @ResponseBody
    public String productxiajia(@RequestParam(value = "id")String id){
        List<Item> itemList=itemMapper.selectByproductid(id);
        for(int u=0;u<itemList.size();u++)
        {
            itemMapper.itemxiajia(itemList.get(u).getItemid());;
        }
        return "下架成功";
    }
    @RequestMapping("/wenitemxiajia")
    @ResponseBody
    public String wenitemxiajia(@RequestParam(value = "id")String id){

        return id;
    }
    @RequestMapping("/itemxiajia")
    @ResponseBody
    public String itemxiajia(@RequestParam(value = "id")String id){
        itemMapper.itemxiajia(id);
        return "下架成功";
    }
    @RequestMapping("/itemshangjia")
    @ResponseBody
    public String itemshangjia(@RequestParam(value = "id")String id){
            itemMapper.itemshangjia(id);
        return "上架成功";
    }
}
